package minio.controller;



import io.minio.errors.*;
import minio.service.FileService;
import minio.util.MinioUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


/**
 * @author Administrator
 */
@RestController
@RequestMapping("/minio")
public class MinioController {

    @Autowired
    private FileService fileService;


    @PostMapping("/upload")
    public String upload(MultipartFile file, String md5) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, XmlParserException, InvalidResponseException, InternalException {
       return fileService.upload(file,md5);
    }

    @GetMapping("/download")
    public void download(HttpServletResponse response,@RequestParam("fileName") String fileName) {
        MinioUtil.download(response, fileName);
    }

    @GetMapping("/preview")
    public void preview(HttpServletResponse response,@RequestParam("fileUrl") String fileUrl) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://47.103.5.48:8012/onlinePreview?url=" + fileUrl;
        restTemplate.getForObject(url, Object.class);
    }



}
